#! /bin/sh
# PCP QA Test No. 119
# pmlogger_check failure produces way too many lines of output
# pv 877570
#
# Copyright (c) 2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

status=1	# failure is the default!
_needclean=true
[ -z "$PCP_PMLOGGERCONTROL_PATH" ] && \
	PCP_PMLOGGERCONTROL_PATH="$PCP_SYSCONF_DIR/pmlogger/control"

LOGGING_DIR=$PCP_LOG_DIR/pmlogger_qa
PMCHECK_LOG=$LOGGING_DIR/pmlogger_check.log

myhost=`hostname`

trap "_cleanup" 0 1 2 3 15

_stop_auto_restart pmcd
_stop_auto_restart pmlogger

_cleanup()
{
    if $_needclean
    then
	[ -f $tmp.control ] && \
	    $sudo cp $tmp.control $PCP_PMLOGGERCONTROL_PATH
	[ -d $PCP_PMLOGGERCONTROL_PATH.d-$seq ] && \
	    $sudo mv $PCP_PMLOGGERCONTROL_PATH.d-$seq $PCP_PMLOGGERCONTROL_PATH.d
	_restore_auto_restart pmcd
	_service pmcd restart 2>&1 | _filter_pcp_restart
	_wait_for_pmcd
	_restore_auto_restart pmlogger
	_service pmlogger restart 2>&1 | _filter_pcp_restart
	_wait_for_pmlogger
	_needclean=false
    fi
    $sudo rm -rf $LOGGING_DIR
    $sudo rm -f $tmp.*
    exit $status
}

_filter()
{
    sed \
	-e "/^ln: failed to create hard link.*SaveLogs/d" \
	-e "s/$myhost/LOCALHOST/g" \
	-e "/UID.*PID/d" \
	-e "/USER.*PID/d" \
	-e "s/.* .*pmlogger .*-c .*/<pmlogger process>/" \
	-e "/Warning: creating directory/s/(.*\/\([^/][^/]*\))/(...\/\1)/" \
    | _filter_pmlogger_log
}

# stop pmcd, primary pmlogger, create the archive directories,
# start pmcd ...
#
_get_pids_by_name -a pmlogger >$tmp.pid
if ! _service pmlogger stop >$tmp.out 2>&1; then _exit 1; fi \
| _filter_pcp_stop
_wait_pmlogger_end || _exit 1
if ! _service pmcd stop >>$tmp.out 2>&1; then _exit 1; fi \
| _filter_pcp_stop
_wait_pmcd_end || _exit 1
cat $tmp.out >$seq_full

# wait for all of the extant pmloggers to exit
#
if [ -s $tmp.pid ]
then
    for pid in `cat $tmp.pid`
    do
	_wait_pmlogger_end $pid || _exit 1
    done
fi

$sudo cp $PCP_PMLOGGERCONTROL_PATH $tmp.control
$sudo mv $PCP_PMLOGGERCONTROL_PATH.d $PCP_PMLOGGERCONTROL_PATH.d-$seq

export PMLOGGER_CHECK_SKIP_LOGCONF=yes

cat <<End-of-File >$tmp.tmp
# dummy file created by qa/$seq on `date`
# the goal here is to have just two pmlogger instances running and
# a third one that will never run ...
#
\$version=1.1
LOCALHOSTNAME	y n $LOGGING_DIR/LOCALHOSTNAME-one -c /dev/null
LOCALHOSTNAME	n n $LOGGING_DIR/LOCALHOSTNAME-two -c /dev/null -L
no.such.host.pcp.io	n n $LOGGING_DIR/no.such.host.pcp.io -c /dev/null
End-of-File
$sudo cp $tmp.tmp $PCP_PMLOGGERCONTROL_PATH

$sudo mkdir -p $LOGGING_DIR/${myhost}-one >/dev/null 2>&1
$sudo mkdir -p $LOGGING_DIR/${myhost}-two >/dev/null 2>&1
$sudo mkdir -p $LOGGING_DIR/no.such.host.pcp.io >/dev/null 2>&1
id pcp >/dev/null 2>&1 && $sudo chown -R $PCP_USER:$PCP_GROUP $LOGGING_DIR
echo "+++ before pcp start ..." >>$seq_full
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '/[p]mlogger( |$)' >>$seq_full
echo "+++" >>$seq_full
if ! _service pmcd start >$tmp.out 2>&1; then _exit 1; fi \
| _filter_pcp_start
_wait_for_pmcd || _exit 1
if ! _service pmlogger start >>$tmp.out 2>&1; then _exit 1; fi \
| _filter_pcp_start
_wait_for_pmlogger || _exit 1
cat $tmp.out >>$seq_full
echo "+++ after pcp start ..." >>$seq_full
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '/[p]mlogger( |$)' >>$seq_full
echo "+++" >>$seq_full

# avoid race with pmloggers not being started before pmlogger_check
# is run below
for i in 1 2 3 4 5 6 7 8 9 10
do
    [ -f $LOGGING_DIR/${myhost}-one/pmlogger.log -a -f $LOGGING_DIR/${myhost}-two/pmlogger.log ] && break
    sleep 1
done
if [ ! -f $LOGGING_DIR/${myhost}-one/pmlogger.log ]
then
    echo "Failed to start pmlogger for ${myhost}-one"
    echo "Logging directory contents ..."
    ls -l $LOGGING_DIR/${myhost}-one
    date
    for log in $PCP_ARCHIVE_DIR/pmlogger_check.log.prev $PCP_ARCHIVE_DIR/pmlogger_check.log
    do
	if [ ! -f "$log" ]
	then
	    echo "$log is missing!"
	else
	    ls -l $log
	    cat $log
	fi
    done
    $PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '/[p]mlogger( |$)'
    exit
fi

if [ ! -f $LOGGING_DIR/${myhost}-two/pmlogger.log ]
then
    echo "Failed to start pmlogger for ${myhost}-two"
    echo "Logging directory contents ..."
    ls -l $LOGGING_DIR/${myhost}-two
    date
    for log in $PCP_ARCHIVE_DIR/pmlogger_check.log.prev $PCP_ARCHIVE_DIR/pmlogger_check.log
    do
	if [ ! -f "$log" ]
	then
	    echo "$log is missing!"
	else
	    ls -l $log
	    cat $log
	fi
    done
    $PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '/[p]mlogger( |$)'
    exit
fi

# real QA test starts here
echo
echo "=== pmlogger_check ==="
echo "+++ before pmlogger_check ..." >>$seq_full
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '/[p]mlogger( |$)' >>$seq_full
echo "+++" >>$seq_full
echo >>$seq_full
echo "=== pmlogger_check ===" >>$seq_full
# need to be a place the user $PCP_USER can cd to from pmlogger_daily
#
cd /var/tmp
$sudo -u $PCP_USER -g $PCP_GROUP $PCP_BINADM_DIR/pmlogger_check -l $PMCHECK_LOG
cd $here
# give any failing pmloggers a chance to exit
#
sleep 2
echo "+++ after pmlogger_check ..." >>$seq_full
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '/[p]mlogger( |$)' >>$seq_full
echo "+++" >>$seq_full
_filter < $PMCHECK_LOG
cat $PMCHECK_LOG >>$seq_full

echo | tee -a $seq_full
echo "=== pmlogger_check -T ===" | tee -a $seq_full
cd /var/tmp
$sudo -u $PCP_USER -g $PCP_GROUP $PCP_BINADM_DIR/pmlogger_check -T -l $PMCHECK_LOG
cd $here
_filter < $PMCHECK_LOG
cat $PMCHECK_LOG >>$seq_full

# all OK
status=0
exit
